package com.crm.user.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.alibaba.fastjson.JSONArray;
import com.google.common.base.Strings;
import com.crm.base.controller.BaseController;
import com.crm.customers.model.ApproveItem;
import com.crm.customers.model.ApproveItemUser;
import com.crm.customers.service.ApproveItemService;
import com.crm.customers.service.ContractMoneylogService;
import com.crm.customers.service.ContractService;
import com.crm.util.Pager;

/**
 * 财务管理controller
 * */
@Controller
public class WorkController extends BaseController {
	
	@Autowired
	private ContractService contractService;
	
	@Autowired
	private ContractMoneylogService contractMoneylogService;
	@Autowired
	private ApproveItemService approveItemService;
 
	/**
	 * 内容审批
	 * @throws UnsupportedEncodingException 
	 * */
	@RequestMapping(value="/workmanage/approveagree", produces="text/html;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public String approveAgree(@ModelAttribute ApproveItem ai,HttpServletResponse response,HttpServletRequest request,
			RedirectAttributes redirectAttributes) throws Exception{
		int result =0;
		try{
			ai.setCreateuser(getLoginUser().getId());
		   result = approveItemService.saveAgreeApprove(ai);
		}catch(Exception e){
			e.printStackTrace();
		}
		
		
		return JSONArray.toJSONString(result);
	}

	
	
	/**
	 * 审核管理
	 * @throws Exception 
	 * */
	@RequestMapping(value="/workmanage/approvelisturl",produces="text/html;charset=UTF-8")
	public String approvelisturl(@ModelAttribute ApproveItem ai,HttpServletResponse response,HttpServletRequest request,
			RedirectAttributes redirectAttributes) throws Exception{
		
		String url = "/workmanage/approvelist";
		url += "_p"+getSkipToPage();
		if (ai.getType() != null) {
			url += "_t"+ai.getType();
		}
		if (ai.getState() != null) {
			url += "_s"+ai.getState();
		}
		if (!Strings.isNullOrEmpty(ai.getKeyWord())){
			url += "?kwd=" + URLEncoder.encode(URLEncoder.encode(ai.getKeyWord(), "UTF-8"),"UTF-8");
		}
		return "redirect:" + url;
	}
	 
	
	/**
	 * 进入审核列表页面
	 * @throws UnsupportedEncodingException 
	 * */
	@RequestMapping(value="/workmanage/approvelist{k}", produces="text/html;charset=UTF-8")
	public String approveList(@PathVariable String k,HttpServletResponse response,HttpServletRequest request,
			RedirectAttributes redirectAttributes) throws Exception{
		Integer pageNo = 1;
		Integer pageSize = 10;
		String keyWord = null;
		ApproveItem ai = new ApproveItem();
		if (k != null) {
			String key[] = k.split("_");
			for (String kk : key) {
				if (kk.indexOf("p") > -1) {
					pageNo = Integer.parseInt(kk.replace("p", ""));
				} else if (kk.indexOf("t") > -1) {
					ai.setType(Integer.parseInt(kk.replace("t", ""))); 
				}
				else if (kk.indexOf("s") > -1) {
					ai.setState(Integer.parseInt(kk.replace("s", ""))); 
				}
			}
		}
		if(request.getParameter("kwd")!=null){  
			keyWord = (URLDecoder.decode(URLDecoder.decode(request.getParameter("kwd"), "UTF-8"),"UTF-8"));
			ai.setKeyWord(keyWord);
		}
		ai.setApproveuser(getLoginUser().getId());
		if(ai.getState()==null){
			ai.setState(1);
		}
		List<ApproveItem>  list =approveItemService.getApproveItemAll(ai, pageNo, pageSize);
		Pager pager = new Pager(pageNo, pageSize);
		pager.setResultList(list);
		pager.setUrl("/workmanage/approvelist"+k);
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("KeyWord",keyWord);
		params.put("state", ai.getState());
		params.put("type", ai.getType());
		pager.setParams(params);
		request.setAttribute("pager", pager); 
		return "workmanage/approvelist";	
	}
	
	

	/**
	 * 我的申请url拼接
	 * @throws Exception 
	 * */
	@RequestMapping(value="/workmanage/myapplylisturl",produces="text/html;charset=UTF-8")
	public String myApplylisturl(@ModelAttribute ApproveItem ai,HttpServletResponse response,HttpServletRequest request,
			RedirectAttributes redirectAttributes) throws Exception{
		
		String url = "/workmanage/myapplylis";
		url += "_p"+getSkipToPage();
		if (ai.getType() != null) {
			url += "_t"+ai.getType();
		}
		if (ai.getState() != null) {
			url += "_s"+ai.getState();
		}
		if (StringUtils.isNotEmpty(ai.getCreatedate())) {
			url += "_a"+ai.getCreatedate();
		}
		if (!Strings.isNullOrEmpty(ai.getKeyWord())){
			url += "?kwd=" + URLEncoder.encode(URLEncoder.encode(ai.getKeyWord(), "UTF-8"),"UTF-8");
		}
		return "redirect:" + url;
	}
	 
	
	/**
	 * 进入我的申请页面
	 * @throws UnsupportedEncodingException 
	 * */
	@RequestMapping(value="/workmanage/myapplylis{k}", produces="text/html;charset=UTF-8")
	public String intoReceivablesList(@PathVariable String k,HttpServletResponse response,HttpServletRequest request,
			RedirectAttributes redirectAttributes) throws Exception{
		Integer pageNo = 1;
		Integer pageSize = 10;
		String keyWord = null;
		ApproveItem ai = new ApproveItem();
		if (k != null) {
			String key[] = k.split("_");
			for (String kk : key) {
				if (kk.indexOf("p") > -1) {
					pageNo = Integer.parseInt(kk.replace("p", ""));
				} else if (kk.indexOf("t") > -1) {
					ai.setType(Integer.parseInt(kk.replace("t", "")));
				}else if (kk.indexOf("a") > -1) {
					ai.setCreatedate(kk.replace("a", ""));
				} 
			}
		}
		if(request.getParameter("kwd")!=null){  
			keyWord = (URLDecoder.decode(URLDecoder.decode(request.getParameter("kwd"), "UTF-8"),"UTF-8"));
			ai.setKeyWord(keyWord);
		}
		ai.setCreateuser(getLoginUser().getId());
		List<ApproveItem>  list =approveItemService.getMyItemAll(ai, pageNo, pageSize);
		Pager pager = new Pager(pageNo, pageSize);
		pager.setResultList(list);
		pager.setUrl("/workmanage/myapplylis"+k);
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("KeyWord",keyWord);
		params.put("state", ai.getState());
		params.put("type", ai.getType());
		params.put("date", ai.getCreatedate());
		pager.setParams(params);
		request.setAttribute("pager", pager); 
		return "workmanage/myapplylis";	
	}
	
	
	
	  
	/**
	 * 所有申请url拼接
	 * @throws Exception 
	 * */
	@RequestMapping(value="/workmanage/allapplylisturl",produces="text/html;charset=UTF-8")
	public String allApplylisturl(@ModelAttribute ApproveItem ai,HttpServletResponse response,HttpServletRequest request,
			RedirectAttributes redirectAttributes) throws Exception{
		
		String url = "/workmanage/allapplylist";
		url += "_p"+getSkipToPage();
		if (ai.getType() != null) {
			url += "_t"+ai.getType();
		}
		if (ai.getState() != null) {
			url += "_s"+ai.getState();
		}
		if (StringUtils.isNotEmpty(ai.getCreatedate())) {
			url += "_a"+ai.getCreatedate();
		}
		if (!Strings.isNullOrEmpty(ai.getKeyWord())){
			url += "?kwd=" + URLEncoder.encode(URLEncoder.encode(ai.getKeyWord(), "UTF-8"),"UTF-8");
		}
		return "redirect:" + url;
	}
	 
	
	/**
	 * 进入我的申请页面
	 * @throws UnsupportedEncodingException 
	 * */
	@RequestMapping(value="/workmanage/allapplylist{k}", produces="text/html;charset=UTF-8")
	public String allApplylist(@PathVariable String k,HttpServletResponse response,HttpServletRequest request,
			RedirectAttributes redirectAttributes) throws Exception{
		Integer pageNo = 1;
		Integer pageSize = 10;
		String keyWord = null;
		ApproveItem ai = new ApproveItem();
		if (k != null) {
			String key[] = k.split("_");
			for (String kk : key) {
				if (kk.indexOf("p") > -1) {
					pageNo = Integer.parseInt(kk.replace("p", ""));
				} else if (kk.indexOf("t") > -1) {
					ai.setType(Integer.parseInt(kk.replace("t", "")));
				}else if (kk.indexOf("a") > -1) {
					ai.setCreatedate(kk.replace("a", ""));
				} 
			}
		}
		if(request.getParameter("kwd")!=null){  
			keyWord = (URLDecoder.decode(URLDecoder.decode(request.getParameter("kwd"), "UTF-8"),"UTF-8"));
			ai.setKeyWord(keyWord);
		}
		List<ApproveItem>  list =approveItemService.getMyItemAll(ai, pageNo, pageSize);
		Pager pager = new Pager(pageNo, pageSize);
		pager.setResultList(list);
		pager.setUrl("/workmanage/allapplylist"+k);
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("KeyWord",keyWord);
		params.put("state", ai.getState());
		params.put("type", ai.getType());
		params.put("date", ai.getCreatedate());
		pager.setParams(params);
		request.setAttribute("pager", pager); 
		return "workmanage/allapplylist";	
	}
	
	

}
